/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ca.ubc.icapture.genapha.actions;

import ca.ubc.icapture.genapha.beans.Login;
import icapture.beans.DB.User;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.tiles.TilesRequestProcessor;

public class CustomLoginRequestAction extends TilesRequestProcessor {

    private String defaultPage = "login.do";

    /**
     * Store the page we are on.
     * This can be used for both redirecting to after accepting the terms of use
     * or after the user logs in it can take them back to the page they selected to login
     * @param request
     * @param response
     * @return
     */
    @Override
    protected boolean processPreprocess(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();

        // Save redirURL
        String redirUrl = request.getServletPath();
        if (!request.getServletPath().equals("/logout.do")
                && !request.getServletPath().equals("/login.do")
                && !request.getServletPath().equals("/loginSubmit.do")) {
            if (request.getQueryString() != null && !request.getQueryString().isEmpty()) {
                redirUrl += "?" + request.getQueryString();
            }
            session.setAttribute("redirUrl", redirUrl);
            if (Login.getLoggedIn(session)) {
                User user = Login.getUser(session);
                if (user == null) {
                    try {
                        // User info is missing.
                        // Something is broken. Relogin.
                        response.sendRedirect(defaultPage);
                    } catch (IOException ex) {
                        Logger.getLogger(CustomLoginRequestAction.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    return false;
                } else {
                    if (Integer.lowestOneBit(user.getDataRights()) == 1) {
                        return true;
                    } else {
                        try {
                            response.sendRedirect(defaultPage);
                        } catch (IOException ex) {
                            Logger.getLogger(CustomLoginRequestAction.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        return false;
                    }
                }
            } else {
                try {
                    // User info is missing.
                    // Something is broken. Relogin.
                    response.sendRedirect(defaultPage);
                } catch (IOException ex) {
                    Logger.getLogger(CustomLoginRequestAction.class.getName()).log(Level.SEVERE, null, ex);
                }
                return false;
            }
        }
        return true;
    }
}
